package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.QuestionDao;
import com.itheima.domain.Question;
import com.itheima.service.QuestionService;
import com.itheima.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.xssf.usermodel.*;

import javax.servlet.ServletOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class QuestionServiceImpl implements QuestionService {
    @Override
    public PageInfo<Question> findByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = null;
        PageInfo<Question> info = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            QuestionDao questionDao = MybatisUtil.getMapper(sqlSession, QuestionDao.class);
            PageHelper.startPage(currentPage,pageSize);
            List<Question> list=questionDao.findAll();
            info = new PageInfo<>(list);
        } finally {
            MybatisUtil.close(sqlSession);
        }

        return info;
    }

    @Override
    public void save(Question question) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            QuestionDao questionDao = MybatisUtil.getMapper(sqlSession, QuestionDao.class);
            String id = UUID.randomUUID().toString();
            question.setId(id);
            questionDao.save(question);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }

    }

    @Override
    public Question findById(String id) {
        SqlSession sqlSession = null;
        Question question= null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            QuestionDao questionDao = MybatisUtil.getMapper(sqlSession, QuestionDao.class);
            question = questionDao.findById(id);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
        return question;
    }

    @Override
    public void update(Question question) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            QuestionDao questionDao = MybatisUtil.getMapper(sqlSession, QuestionDao.class);

            questionDao.update(question);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }

    }

    @Override
    public void delete(String[] ids) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            QuestionDao questionDao = MybatisUtil.getMapper(sqlSession, QuestionDao.class);
            Stream.of(ids).forEach(id->questionDao.delete(id));
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public List<Question> findAll() {
        SqlSession sqlSession = null;
        List<Question> list = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            QuestionDao questionDao = MybatisUtil.getMapper(sqlSession, QuestionDao.class);
            list=questionDao.findAll();

        } finally {
            MybatisUtil.close(sqlSession);
        }

        return list;
    }

    @Override
    public void toExport(OutputStream outputStream) throws IOException, InvalidFormatException {
        String path = this.getClass().getClassLoader().getResource("question_template.xlsx").getPath();
        XSSFWorkbook workbook = new XSSFWorkbook(new File(path));
        XSSFSheet sheet = workbook.getSheetAt(0);
        List<Question> questionList = findAll();
        List<String[]> questions = questionList.stream().map(question -> question.toString().split(",")).collect(Collectors.toList());
        XSSFCellStyle style = workbook.createCellStyle();
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        int rowIndex = 3;
        for (String[] fields : questions) {
            XSSFRow row = sheet.createRow(rowIndex++);
            int cellIndex = 1;
            for (String field : fields) {
                XSSFCell cell = row.createCell(cellIndex++);
                cell.setCellValue(field);
                cell.setCellStyle(style);
            }
        }
        workbook.write(outputStream);
        workbook.close();
    }
}
